package com.nuoniu.sibanyun.service.crm.call.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.entity.crm.call.CrmCallBaseSettingInfo;
import com.nuoniu.sibanyun.entity.crm.call.CrmCallBill;
import com.nuoniu.sibanyun.entity.crm.call.dto.CrmCallBillDto;
import com.nuoniu.sibanyun.mapper.crm.call.CrmCallBillMapper;
import com.nuoniu.sibanyun.service.crm.call.ICrmCallBaseSettingInfoService;
import com.nuoniu.sibanyun.service.crm.call.ICrmCallBillService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>
 * 客户管理-外呼账单 服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2022-05-06
 */
@Service
public class CrmCallBillServiceImpl extends ServiceImpl<CrmCallBillMapper, CrmCallBill> implements ICrmCallBillService {

    @Autowired
    private ICrmCallBaseSettingInfoService callBaseSettingInfoService;

    @Override
    public Page<CrmCallBill> pageList(CrmCallBillDto crmCallBillDto) {
        UserInfo userInfo = UserThreadLocal.get();
        Integer companyId = userInfo.getCompanyId();
        Page page = crmCallBillDto.getPage();
        Page<CrmCallBill> crmCallBillPage = baseMapper.pageList(page, companyId, crmCallBillDto);
        return crmCallBillPage;
    }

    @Override
    public CrmCallBill getCurrentBillByCompany(Integer companyId) {
        UserInfo userInfo = UserThreadLocal.get();
        if(null == companyId){
            companyId = userInfo.getCompanyId();
        }
        CrmCallBill currentBillByCompany = baseMapper.getCurrentBillByCompany(companyId);
        return currentBillByCompany;
    }

    @Override
    public void checkCurrentBill(Integer companyId) {
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        CrmCallBill crmCallBill = this.getCurrentBillByCompany(companyId);
        if(null != crmCallBill) {
            return ;
        }
        CrmCallBaseSettingInfo baseSettingInfo = callBaseSettingInfoService.getByCompany(companyId);
        crmCallBill = new CrmCallBill();
        crmCallBill.setCompanyId(companyId);
        crmCallBill.setCrmGoodsId(baseSettingInfo.getCrmGoodsId());
        crmCallBill.setCrmGoodsName(baseSettingInfo.getCrmGoodsName());
        crmCallBill.setBaseMinutes(baseSettingInfo.getBaseMinutes());
        crmCallBill.setBaseCharges(baseSettingInfo.getBaseCharges());
        crmCallBill.setOrderAmount(baseSettingInfo.getBaseCharges());
        crmCallBill.setBaseMinutesUsed(0);
        crmCallBill.setPrice(baseSettingInfo.getPrice());

        crmCallBill.setOtherMinutes(0);
        crmCallBill.setCreateTime(now);
        crmCallBill.setStatus(0);
        crmCallBill.setYear(year + "年");
        crmCallBill.setMonth(month + "月");
        this.save(crmCallBill);
    }

    @Override
    public void checkCurrentBillFromBaseSettingInfo(CrmCallBaseSettingInfo baseSettingInfo) {
        Integer companyId = baseSettingInfo.getCompanyId();
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        CrmCallBill crmCallBill = this.getCurrentBillByCompany(companyId);
        if(null != crmCallBill) {
            return ;
        }
        crmCallBill = new CrmCallBill();
        crmCallBill.setCompanyId(companyId);
        crmCallBill.setCrmGoodsId(baseSettingInfo.getCrmGoodsId());
        crmCallBill.setCrmGoodsName(baseSettingInfo.getCrmGoodsName());
        crmCallBill.setBaseMinutes(baseSettingInfo.getBaseMinutes());
        crmCallBill.setBaseCharges(baseSettingInfo.getBaseCharges());
        crmCallBill.setOrderAmount(baseSettingInfo.getBaseCharges());
        crmCallBill.setBaseMinutesUsed(0);
        crmCallBill.setPrice(baseSettingInfo.getPrice());

        crmCallBill.setOtherMinutes(0);
        crmCallBill.setCreateTime(now);
        crmCallBill.setStatus(0);
        crmCallBill.setYear(year + "年");
        crmCallBill.setMonth(month + "月");
        this.save(crmCallBill);
    }


}
